@{
    ViewData["Title"] = "选择供应商";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>@ViewData["Title"]</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="~/layui/css/layui.css">
</head>
<body>
    <div style="padding: 20px;">
        <!-- 查询条件 -->
        <div class="layui-form" lay-filter="searchForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">供应商编码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="supplierCode" placeholder="请输入供应商编码" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">供应商名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="supplierName" placeholder="请输入供应商名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search"></i> 查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary">
                        <i class="layui-icon layui-icon-refresh"></i> 重置
                    </button>
                </div>
            </div>
        </div>

        <!-- 数据表格 -->
        <table class="layui-hide" id="supplierTable" lay-filter="supplierTable"></table>

        <!-- 操作按钮 -->
        <div style="text-align: center; margin-top: 20px;">
            <button type="button" class="layui-btn" id="btnConfirm">确定选择</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="parent.layer.closeAll()">取消</button>
        </div>
    </div>

    <script src="~/layui/layui.js"></script>
    <script>
        layui.use(['table', 'form', 'layer'], function () {
            var $ = layui.jquery;
            var table = layui.table;
            var form = layui.form;
            var layer = layui.layer;

            var selectedSupplier = null;

            // 初始化表格
            var tableIns = table.render({
                elem: '#supplierTable',
                url: '/Procurement/GetSupplierSelect',
                cols: [[
                    { type: 'radio', width: 60 },
                    { field: 'supplierCode', title: '供应商编码', width: 150 },
                    { field: 'supplierName', title: '供应商名称', width: 200 },
                    { field: 'contactPerson', title: '联系人', width: 120 },
                    { field: 'contactPhone', title: '联系电话', width: 150 },
                    { field: 'address', title: '地址', width: 200 }
                ]],
                page: true,
                limit: 10,
                limits: [10, 20, 50]
            });

            // 查询
            form.on('submit(search)', function (data) {
                var field = data.field;
                tableIns.reload({
                    where: field,
                    page: {
                        curr: 1
                    }
                });
                return false;
            });

            // 监听单选
            table.on('radio(supplierTable)', function (obj) {
                selectedSupplier = obj.data;
            });

            // 确定选择
            $('#btnConfirm').on('click', function () {
                if (!selectedSupplier) {
                    layer.msg('请选择一个供应商');
                    return;
                }

                // 调用父页面的回调函数
                if (parent.window.selectSupplierCallback) {
                    parent.window.selectSupplierCallback(selectedSupplier);
                }
                
                // 只关闭当前供应商选择对话框
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            });
        });
    </script>
</body>
</html> 